package zdyu.utils;


import java.util.Arrays;
import java.util.Iterator;
import java.util.StringJoiner;

public class GenericsArray<E> implements Iterable<E> {

    public E[] data;

    @SuppressWarnings("unchecked")
    public GenericsArray(int size){
        data = (E[]) new Object[size];
    }

    public void put(int index, E item) {
        data[index] = item;
    }

    public E get(int index) {
        return data[index];
    }

    public int length() { return data.length; }


    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            int nextIndex = 0;

            @Override
            public boolean hasNext() {
                return nextIndex < data.length;
            }

            @Override
            public E next() {
                E result =  get(nextIndex);
                nextIndex++;
                return result;
            }
        };
    }

    @Override
    public String toString() {
        return Arrays.toString(data);
    }
}
